/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PatVisitIndex;
import com.jims.master.modules.doctor.entity.PregnantRecords;
import com.jims.master.modules.doctor.service.ClinicMasterService;
import com.jims.master.modules.doctor.service.PregnantRecordsService;
import com.jims.master.modules.oper.entity.DiseaseManagement;
import com.jims.master.modules.sys.entity.Dict;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 孕妇手册-初诊记录Controller
 * @author 王志明
 * @version 2017-07-18
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/pregnantRecords")
public class PregnantRecordsController extends BaseController {

	@Autowired
	private PregnantRecordsService PregnantRecordsService;
    @Autowired
    private ClinicMasterService clinicMasterService;
	@ModelAttribute
	public PregnantRecords get(@RequestParam(required=false) String id) {
		PregnantRecords entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = PregnantRecordsService.get(id);
		}
		if (entity == null){
			entity = new PregnantRecords();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(PregnantRecords pregnantRecords,String clinicId, String patientId, HttpServletRequest request, HttpServletResponse response, Model model) {
        ClinicMaster clinicMaster = clinicMasterService.get(clinicId);
        pregnantRecords.setClinicId(clinicId);
        Page<PregnantRecords> page = PregnantRecordsService.findPage(new Page<PregnantRecords>(request, response), pregnantRecords);
		model.addAttribute("page", page);
		model.addAttribute("clinicMaster", clinicMaster);
		model.addAttribute("pregnantRecords", pregnantRecords);
        return "modules/doctor/pregnantManual/pregnantRecordsIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(PregnantRecords PregnantRecords, Model model) {
		model.addAttribute("PregnantRecords", PregnantRecords);
		return "modules/pregnantrecords/PregnantRecordsForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(PregnantRecords PregnantRecords, Model model) {
	    StringData data=new StringData();
        if(PregnantRecords.getHrs().equals("1")){
            PregnantRecords.setHrsReason("");
        }
        if(PregnantRecords.getReferral().equals("1")){
            PregnantRecords.setReferralCompany("");
            PregnantRecords.setReferralReason("");
        }
		int i=PregnantRecordsService.save(PregnantRecords);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(PregnantRecords pregnantRecords) {
	    StringData data=new StringData();
       /* PregnantRecords pregnantRecords=new PregnantRecords();
        pregnantRecords.setId(id);*/
		int i=PregnantRecordsService.delete(pregnantRecords);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}
    /**
     * @Author chenxy
     * @desc 查看初诊记录
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findById", ""})
    public String findById(String clinicId,String id,String visitNum, String patientId,HttpServletRequest request, HttpServletResponse response, Model model) {
        ClinicMaster clinicMaster = clinicMasterService.get(clinicId);
        PregnantRecords pregnantRecords = this.PregnantRecordsService.get(id);
        String orgId= UserUtils.getOrgId();
        String orgName=PregnantRecordsService.getOrgName(orgId);
        String deptName=UserUtils.getUser().getOrgDept().getDeptName();
        String doctorName=UserUtils.getUser().getName();
        if(pregnantRecords==null){
            pregnantRecords=new PregnantRecords();
            pregnantRecords.setVisitNum(visitNum);
            pregnantRecords.setReferral("1");
            pregnantRecords.setHrs("1");
            pregnantRecords.setGestationalWeeks("0");
            pregnantRecords.setPhGestationalNum("0");
            pregnantRecords.setPhBearingNum("0");
            pregnantRecords.setPhVaginalDelivery("0");
            pregnantRecords.setPhCesareanSection("0");
            pregnantRecords.setPhFullTerm("0");
            pregnantRecords.setPhCast("0");
            pregnantRecords.setPhOverdue("0");
            pregnantRecords.setPhNaturalAbortion("0");
            pregnantRecords.setPhArtificialAbortion("0");
            pregnantRecords.setPhStillbirth("0");
            pregnantRecords.setPhDeadBirth("0");
            pregnantRecords.setPhBirthDefect("0");
            pregnantRecords.setPhNeonatalDeath("0");
            pregnantRecords.setGexamHeart("未见异常");
            pregnantRecords.setGexamLungs("未见异常");
            pregnantRecords.setGexamLiver("未见异常");
            pregnantRecords.setGexamSpleen("未见异常");
            pregnantRecords.setGexamLimbs("未见异常");
            pregnantRecords.setGyexamVulva("未见异常");
            pregnantRecords.setGyexamVagina("未见异常");
            pregnantRecords.setGyexamCervical("未见异常");
            pregnantRecords.setGyexamUterus("未见异常");
            pregnantRecords.setGyexamUterineAdnexa("未见异常");
            pregnantRecords.setLabRbt("正常");
            pregnantRecords.setLabUrineRoutine("正常");
            pregnantRecords.setLabLiverFunction("正常");
            pregnantRecords.setLabRenalFunction("正常");
            pregnantRecords.setLabUltrasound("正常");
            pregnantRecords.setAppointmentDate(clinicMaster.getVisitDate());
        }
        pregnantRecords.setInspectionUnit(orgName);
        pregnantRecords.setDeptName(deptName);
        pregnantRecords.setDoctorName(doctorName);
        pregnantRecords.setDoctorUser(UserUtils.getUser().getId());
        model.addAttribute("pregnantRecords", pregnantRecords);
        model.addAttribute("clinicMaster", clinicMaster);
        return "modules/doctor/pregnantManual/pregnantRecordsForm";
    }
    /**
     * 从数据字典获取值赋给多选下拉列表
     * @param q
     * @param limit
     * @param model
     * zp
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "automatic")
    @ResponseBody
    public String  automatic(String type,String q,String limit,Model model,String ids) {
        String[] id = null;
        if(!"".equals(ids) && ids != null){
            id = ids.split(";");
        }
        String retJson= "";
        List<Dict> list = PregnantRecordsService.findListDict(type,id,q, limit);
        String json= "";
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                json+= "{\"id\":\""+list.get(i).getValue()+"\",\"text\":\""+list.get(i).getLabel()+"\"},";
            }
            json = json.substring(0,json.length()-1);
            retJson= "{\"items\": ["+json+"]}";
        }

        return retJson;
    }
}